package com.hsmw.common.base;


import com.htn.common.core.base.DataResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.*;

/**
 * @author hongfei
 */
@Primary
@Slf4j
public abstract class BaseController <Bs extends BaseService, M extends BaseModel> {

    @Autowired
    protected Bs baseService;

    @PostMapping
    public DataResponse<M> add(@RequestBody M entity){
        baseService.save(entity);
        return new DataResponse<>();
    }
    @GetMapping("/{id}")
    public DataResponse<M> get(@PathVariable String id){
        Object o = baseService.getById(id);
        DataResponse<M> objectResponse = new DataResponse<>();
        if (o!=null){
            objectResponse.setData((M) o);
        }
        return objectResponse;
    }

    @PutMapping
    public DataResponse<M> update(M entity){
        baseService.updateById(entity);
        return new DataResponse<>();
    }

    @DeleteMapping("/{id}")
    public DataResponse<M> delete(@PathVariable String id){
        baseService.removeById(id);
        return new DataResponse<>();
    }
}
